package ai.equipmentManagers;

import java.util.List;

import fileHandling.language.Lang;
import fileHandling.language.properties.NodeProps;
import fileHandling.language.properties.WeaponProps;

import logic.common.player.Player;
import logic.common.team.Team;
import logic.items.ItemProperties;
import logic.items.ItemType;
import logic.items.RepairItemProperties;
import logic.items.ShieldItemProperties;
import logic.nodes.nodeSettings.upgrades.UpgradableSettings;
import logic.ships.hunter.Hunter;
import logic.weapons.Weapon;
import logic.weapons.WeaponType;
import logic.weapons.projectileWeapons.ProjectileWeapon;

public abstract class EquipmentManager {
	
	protected Player bot;
	protected RepairItemProperties repairItemProps;
	protected ShieldItemProperties shieldItemProps;
	
	public EquipmentManager(Player bot) {
		this.bot = bot;
		
		Team team = bot.getTeam();
		
		team.getItemProperties();
		repairItemProps = (RepairItemProperties)team.getItemProp(ItemType.Repair);
		shieldItemProps = (ShieldItemProperties)team.getItemProp(ItemType.Shields);
	}
	
	public abstract void manageEquipment();
	
	protected void buyItem(ItemProperties itemProps, int max) {
		int costs = itemProps.getCosts();
		Hunter hunter = bot.getHunter();
		
		if(hunter.getItemsAmount(itemProps.getType()) < max && bot.getMoney() >= costs) {
			bot.spendMoney(costs);
			hunter.addItem(itemProps);
		}
	}
	
	protected void restock(WeaponType type) {
		Hunter hunter = bot.getHunter();
		List<Weapon> weapons = hunter.getWeapons(type);
		
		int projectiles = 0;
		for(Weapon w : weapons) {
			ProjectileWeapon pWeapon = (ProjectileWeapon)w;
			projectiles += pWeapon.getMaxProjectiles() - pWeapon.getProjectilesLeft();
		}
		
		int projectileCosts = ProjectileWeapon.getProjectileCosts(type);
		int costs = projectiles * projectileCosts;
		
		int botMoney = bot.getMoney();
		if(costs > botMoney) {
			int diff = costs - botMoney;
			projectiles -= (diff / projectileCosts) + 1;
			costs = projectiles * projectileCosts;
		}
		
		if(projectiles > 0) {
			bot.spendMoney(costs);
			hunter.restockProjectiles(type, projectiles);
		}
	}
	
	protected void upgradeWeapon(Weapon weapon, WeaponProps prop) {
		upgrade(weapon.getWeaponProperties(), prop);
	}
	
	protected void upgradeHunter(Hunter hunter, NodeProps prop) {
		upgrade(hunter.getHunterProperties(), prop);
	}
	
	protected void upgrade(UpgradableSettings settings, Lang prop) {
		String propString = prop.toString();
		bot.spendMoney(settings.getUpgradeCost(propString));
		settings.upgrade(propString);
	}
}